<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="部门" prop="subordinateUnits">
        <treeselect v-model="queryParams.subordinateUnits" :options="deptOptions" :show-count="true" placeholder="请选择部门" style="width: 180px"/>

      </el-form-item>
<!--      <el-form-item label="数量" prop="quantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.quantity"-->
<!--          placeholder="请输入数量"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="车辆名称" prop="vehicleName">
        <el-input
          v-model="queryParams.vehicleName"
          placeholder="请输入车辆名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width: 180px"
        />
      </el-form-item>
      <el-form-item label="车辆类型" prop="vehicleModel">
        <el-input
          v-model="queryParams.vehicleModel"
          placeholder="请输入车辆类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width: 180px"
        />
      </el-form-item>
<!--      <el-form-item label="罐车容量" prop="tankTruckCapacity">-->
<!--        <el-input-->
<!--          v-model="queryParams.tankTruckCapacity"-->
<!--          placeholder="请输入罐车容量"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="出厂日期" prop="productionDate">-->
<!--        <el-date-picker clearable size="small"-->
<!--          v-model="queryParams.productionDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="选择出厂日期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="出厂编号" prop="factoryNumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.factoryNumber"-->
<!--          placeholder="请输入出厂编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="车牌号" prop="plateNumber">
        <el-input
          v-model="queryParams.plateNumber"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width: 180px"
        />
      </el-form-item>
<!--      <el-form-item label="底盘规格型号" prop="chassisSpecification">-->
<!--        <el-input-->
<!--          v-model="queryParams.chassisSpecification"-->
<!--          placeholder="请输入底盘规格型号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="底盘号" prop="chassisNumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.chassisNumber"-->
<!--          placeholder="请输入底盘号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="制造厂家" prop="manufacturer">-->
<!--        <el-input-->
<!--          v-model="queryParams.manufacturer"-->
<!--          placeholder="请输入制造厂家"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="车辆自编号" prop="vehicleNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.vehicleNo"-->
<!--          placeholder="请输入车辆自编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--          style="width: 180px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="已使用年限" prop="usedYears">-->
<!--        <el-input-->
<!--          v-model="queryParams.usedYears"-->
<!--          placeholder="请输入已使用年限"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="还可使用年限" prop="canUsedYears">-->
<!--        <el-input-->
<!--          v-model="queryParams.canUsedYears"-->
<!--          placeholder="请输入还可使用年限"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否进口" prop="isImport">-->
<!--        <el-input-->
<!--          v-model="queryParams.isImport"-->
<!--          placeholder="请输入是否进口"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用" prop="remarkSpare">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare"-->
<!--          placeholder="请输入备用"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用1" prop="remarkSpare1">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare1"-->
<!--          placeholder="请输入备用1"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用2" prop="remarkSpare2">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarkSpare2"-->
<!--          placeholder="请输入备用2"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ledger:vehicles:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ledger:vehicles:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ledger:vehicles:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['ledger:vehicles:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehiclesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="部门" align="center" prop="subordinateUnits" />
      <el-table-column label="车辆名称" align="center" prop="vehicleName" />
      <el-table-column label="车辆类型" align="center" prop="vehicleModel" />
      <el-table-column label="车辆数量" align="center" prop="quantity" />
      <el-table-column label="车牌号" align="center" prop="plateNumber" />
      <el-table-column label="容量(升)" align="center" prop="tankTruckCapacity" />
            <el-table-column label="长度(米)" align="center" prop="remarkSpare" />
            <el-table-column label="宽度(米)" align="center" prop="remarkSpare1" />
            <el-table-column label="高度(米)" align="center" prop="remarkSpare2" />
      <el-table-column label="制造厂家" align="center" prop="manufacturer" />
      <el-table-column label="出厂日期" align="center" prop="productionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已使用年限" align="center" prop="usedYears" />
      <el-table-column label="底盘号" align="center" prop="chassisNumber" />
<!--      <el-table-column label="出厂编号" align="center" prop="factoryNumber" />-->
<!--      <el-table-column label="底盘规格型号" align="center" prop="chassisSpecification" />-->
<!--      <el-table-column label="车辆自编号" align="center" prop="vehicleNo" />-->
<!--      <el-table-column label="还可使用年限" align="center" prop="canUsedYears" />-->
<!--      <el-table-column label="是否进口" align="center" prop="isImport" :formatter="isImportFormat" />-->
<!--      <el-table-column label="备注" align="center" prop="remark" />-->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:vehicles:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ledger:vehicles:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改特种车辆清单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
        <el-form-item label="部门" prop="subordinateUnits">
<!--          <el-input v-model="form.subordinateUnits" placeholder="请输入所属单位名称" />-->
          <treeselect v-model="form.subordinateUnits" :options="deptOptions" :show-count="true" placeholder="请选择部门"  style="width: 100%"/>
        </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="车辆名称" prop="vehicleName">
              <el-input v-model="form.vehicleName" placeholder="请输入车辆名称" />
            </el-form-item>

          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="车辆类型" prop="vehicleModel">
              <el-input v-model="form.vehicleModel" placeholder="请输入车辆类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">

            <el-form-item label="车辆数量" prop="quantity">
              <el-input-number v-model="form.quantity" placeholder="请输入车辆数量" style="width: 100%"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="车牌号" prop="plateNumber">
              <el-input v-model="form.plateNumber" placeholder="请输入车牌号" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="容量(升)" prop="tankTruckCapacity">
              <el-input-number v-model="form.tankTruckCapacity" :precision="3" placeholder="请输入容量" style="width: 100%"/>
            </el-form-item>
          </el-col>
        </el-row>
          <el-row :gutter="20">
            <el-col :span="12" :xs="24">
              <el-form-item label="长度(米)" prop="remarkSpare">
                <el-input-number v-model="form.remarkSpare" :precision="3" placeholder="请输入长度" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" :xs="24">
              <el-form-item label="宽度(米)" prop="remarkSpare1">
                <el-input-number v-model="form.remarkSpare1" :precision="3" placeholder="请输入宽度" style="width: 100%"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12" :xs="24">
              <el-form-item label="高度(米)" prop="remarkSpare2">
                <el-input-number v-model="form.remarkSpare2" :precision="3" placeholder="请输入高度" style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" :xs="24">
              <el-form-item label="制造厂家" prop="manufacturer">
                <el-input v-model="form.manufacturer" placeholder="请输入制造厂家" />
              </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="出厂日期" prop="productionDate">
              <el-date-picker clearable size="small"
                              v-model="form.productionDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择出厂日期" style="width: 100%" @change="usedYears">
              </el-date-picker>
            </el-form-item>

          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="已使用年限" prop="usedYears">
              <el-input v-model="form.usedYears" placeholder="请输入已使用年限" style="width: 100%"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="底盘号" prop="chassisNumber">
              <el-input v-model="form.chassisNumber" placeholder="请输入底盘号" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="底盘规格型号" prop="chassisSpecification">
              <el-input v-model="form.chassisSpecification" placeholder="请输入底盘规格型号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="出厂编号" prop="factoryNumber">
              <el-input v-model="form.factoryNumber" placeholder="请输入出厂编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
        <el-form-item label="车辆自编号" prop="vehicleNo">
          <el-input v-model="form.vehicleNo" placeholder="请输入车辆自编号" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="还可使用年限" prop="canUsedYears">
              <el-input v-model="form.canUsedYears" placeholder="请输入还可使用年限" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="是否进口" prop="isImport">
              <!--          <el-input v-model="form.isImport" placeholder="请输入是否进口" />-->
              <el-select v-model="form.isImport" placeholder="请输入是否进口" style="width: 100%">
                <el-option
                  v-for="dict in isImportOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVehicles, getVehicles, delVehicles, addVehicles, updateVehicles, exportVehicles } from "@/api/ledger/vehicles";
import Treeselect from '@riophae/vue-treeselect'
import { treeselect } from "@/api/system/dept";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getCurrentUser } from "@/api/system/user";

export default {
  name: "Vehicles",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 特种车辆清单表格数据
      vehiclesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否进口字典
      isImportOptions: [],
      // 部门树选项
      deptOptions: undefined,
      deptId: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        subordinateUnits: null,
        quantity: null,
        vehicleName: null,
        vehicleModel: null,
        tankTruckCapacity: null,
        productionDate: null,
        factoryNumber: null,
        plateNumber: null,
        chassisSpecification: null,
        chassisNumber: null,
        manufacturer: null,
        vehicleNo: null,
        usedYears: null,
        canUsedYears: null,
        isImport: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        subordinateUnits: [
          { required: true, message: "部门不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        vehicleName: [
          { required: true, message: "车辆名称不能为空", trigger: "blur" }
        ],
        vehicleModel: [
          { required: true, message: "车辆类型不能为空", trigger: "blur" }
        ],
        tankTruckCapacity: [
          { required: true, message: "罐车容量不能为空", trigger: "blur" }
        ],
        productionDate: [
          { required: true, message: "出厂日期不能为空", trigger: "blur" }
        ],
        factoryNumber: [
          { required: true, message: "出厂编号不能为空", trigger: "blur" }
        ],
        plateNumber: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        chassisSpecification: [
          { required: true, message: "底盘规格型号不能为空", trigger: "blur" }
        ],
        chassisNumber: [
          { required: true, message: "底盘号不能为空", trigger: "blur" }
        ],
        manufacturer: [
          { required: true, message: "制造厂家不能为空", trigger: "blur" }
        ],
        vehicleNo: [
          { required: true, message: "车辆自编号不能为空", trigger: "blur" }
        ],
        usedYears: [
          { required: true, message: "已使用年限不能为空", trigger: "blur" }
        ],
        canUsedYears: [
          { required: true, message: "还可使用年限不能为空", trigger: "blur" }
        ],
        isImport: [
          { required: true, message: "是否进口不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getCurrentUser();
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.isImportOptions = response.data;
    });
    this.getTreeselect();
  },
  methods: {
    /** 查询特种车辆清单列表 */
    getList() {
      this.loading = true;
      listVehicles(this.queryParams).then(response => {
        this.vehiclesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
        // console.log('this.deptOptions:' + JSON.stringify(this.deptOptions))
      });
    },
    getCurrentUser() {
      let currentUser = this.$store.state.user;
      getCurrentUser(currentUser.userId).then(response => {
        // this.createBy = response.data.userId
        this.deptId = response.data.deptId
      });
    },
    // 是否进口字典翻译
    isImportFormat(row, column) {
      return this.selectDictLabel(this.isImportOptions, row.isImport);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        subordinateUnits: null,
        quantity: null,
        vehicleName: null,
        vehicleModel: null,
        tankTruckCapacity: null,
        productionDate: null,
        factoryNumber: null,
        plateNumber: null,
        chassisSpecification: null,
        chassisNumber: null,
        manufacturer: null,
        vehicleNo: null,
        usedYears: null,
        canUsedYears: null,
        isImport: null,
        remark: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.subordinateUnits = this.deptId;
      this.open = true;
      this.title = "添加特种车辆清单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVehicles(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改特种车辆清单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVehicles(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVehicles(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除特种车辆清单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delVehicles(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有特种车辆清单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          // return exportVehicles(queryParams);
        this.download('/ledger/vehicles/export', {
          ...this.queryParams
        }, `特种车辆清单.xlsx`)
        this.exportLoading = false;
        }).then(response => {
          // this.download(response.msg);
          // this.exportLoading = false;
        }).catch(() => {});
    },
    usedYears(value){
      const startDate = new Date(value)
      const endDate = new Date();
      let year = endDate.getFullYear() - startDate.getFullYear();
      let months = (endDate.getFullYear() * 12 + endDate.getMonth()) - (startDate.getFullYear() * 12 + startDate.getMonth());
      // 如果结束日期在开始日期之后一年以上，需要减去余年的月份
      if (endDate.getFullYear() - startDate.getFullYear() > 1) {
        months -= 12 * (endDate.getFullYear() - startDate.getFullYear());
      }
      // let time = ((endDate.getFullYear() * 12 + endDate.getMonth()) - (startDate.getFullYear() * 12 + startDate.getMonth()))/12;
      // console.log(time);
      console.log(year);
      console.log(months);
      let msg = "";
      if (year > 0){
        msg += year + '年';
      }
      if (months > 0){
        msg += months + '月';
      }
      this.form.usedYears = msg;
    }
  }
};
</script>
